package com.zjj.distributed.zk.application.lock;

import com.zjj.distributed.biz.OrderCodeGenerator;
import com.zjj.lbw.common.lock.Lock;

/**
 * @author zhanglei.zjj
 * @description Zookeeper实现分布式锁测试类
 * @date 2023/6/22 15:54
 */
public class ZkLockTest implements Runnable {
    private OrderCodeGenerator orderCodeGenerator = new OrderCodeGenerator();

//    private  Lock lock = new ZkDistributedLock1("192.168.200.129:2181", "/locks");
    private Lock lock = new ZkDistributedLock2("192.168.200.129:2181", "/locks");

    @Override
    public void run() {

        lock.lock();
        try {
            String orderCode = orderCodeGenerator.getOrderCode();
            System.out.println("生成订单号 " + orderCode);
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 30; i++) {
            new Thread(new ZkLockTest()).start();
        }

        Thread.currentThread().join();
    }
}
